﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZK.Common;
using ZK.InterfaceServer;

namespace ZK.HardWareBLL
{
    /// <summary>
    /// 上海耀华xk3190A9 
    /// 兼容 宁波柯力d2008
    /// </summary>
    public class A9WagonBalanceBLL
    {
        SerialPort serialPort;
        HaredWareCommonM haredWareCommonM;
        double nowWeight;
        public A9WagonBalanceBLL()
        {
            nowWeight = 0;
            serialPort = new SerialPort();
            haredWareCommonM = Common.IConfig.GetSetwagonBalance;
            Open();
        }

        public void Open()
        {
            try
            {
                if (!serialPort.IsOpen)
                {
                    serialPort.PortName = haredWareCommonM.PortName;
                    serialPort.BaudRate = Convert.ToInt32(haredWareCommonM.BaudRate);
                    serialPort.Parity = Parity.None;
                    serialPort.DataBits = Convert.ToInt32(haredWareCommonM.DataBits);
                    serialPort.StopBits = StopBits.One;
                    //ComDevice.ReadBufferSize = 20;
                    serialPort.Open();
                    serialPort.DataReceived += SerialPort_DataReceived;
                    // ComDevice.ErrorReceived += ComDevice_ErrorReceived;
                    serialPort.ReceivedBytesThreshold = 36;
                    serialPort.DtrEnable = true;
                }
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.ToString());
                AutoControlTransportApiServer.AddHardWareLog(haredWareCommonM.Name, 1, "地磅打开失败！");
                DelegateHelper.InfoMessageEvent?.Invoke(ex.Message);
            }
        }

        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] bytes = new byte[serialPort.BytesToRead];
            serialPort.Read(bytes, 0, serialPort.BytesToRead);

            byte[] bbz = null;
            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] == 2)
                {
                    bbz = bytes.Skip(i).Take(12).ToArray();
                    goto A;
                }
            }
        A:
            if (bbz != null)
            {
                double weight = GetWeight(bbz);
                if ((weight != nowWeight) && (weight != -1))
                {
                    nowWeight = weight;
                    DelegateHelper.A9Event?.Invoke(GetWeight(bbz));
                }
            }

        }

        private double GetWeight(byte[] bytes)
        {
            // Common.Log4.LogManage.WriteInfo(string.Join("-", bytes));
            if (bytes.Length == 12 && (bytes[0] == 2) && (bytes[11] == 3))
            {
                var number = 0;
                number += (bytes[2] - 48) * 100000;
                number += (bytes[3] - 48) * 10000;
                number += (bytes[4] - 48) * 1000;
                number += (bytes[5] - 48) * 100;
                number += (bytes[6] - 48) * 10;
                number += (bytes[7] - 48);
                return (double)number / 1000;
                //return number;
            }
            else
            {
                Common.Log4.LogManage.WriteErr("获取地磅数据长度有误 errDate:" + string.Join("-", bytes));
                return -1;
            }
        }

        ~A9WagonBalanceBLL()
        {
            try
            {
                if (serialPort.IsOpen)
                {
                    serialPort.Close();
                }
            }
            catch (Exception ex)
            {

                Common.Log4.LogManage.WriteErr(ex.ToString());
            }
        }
    }
}
